// pages/home/index.js
import * as echarts from '../../../ec-canvas/echarts.min.js';
let chart = null;
const app = getApp()

function initChart(canvas, width, height, dpr) {
  const chart = echarts.init(canvas, null, {
    width: width,
    height: height,
    devicePixelRatio: dpr // new
  });
  canvas.setChart(chart);

  var option = {
    backgroundColor: "#ffffff",
    color: ["#37A2DA", "#17BB89"],
    series: [{
      type: 'pie',
      radius: ['40%', '60%'],
      label: {
        normal: {
          show: true,
          position: 'center',
          color:'#17BB89',
          formatter: '一般'+ '\n\r' + '（健康度）',
          rich: {
              total:{
                  fontSize: 12,
                  fontFamily : "微软雅黑",
                  color:'#17BB89'
              },
              active: {
                  fontFamily : "微软雅黑",
                  fontSize: 18,
                  color:'#17BB89',
                  lineHeight:30,
              },
          }
      },
      emphasis: {//中间文字显示
          show: true,
      }
    },
      data: [{
        value: 55
      }, {
        value: 20
      }]
    }]
  };

  chart.setOption(option);
  return chart;
}

Page({

  /**
   * 页面的初始数据
   */
  data: {
    weatherData: [
      {
        text: '晴转多云',
        path: '../../../images/icon8.png'
      },
      {
        text: '武汉',
        path: '../../../images/icon3.png'
      },
      {
        text: '25.3℃',
        path: '../../../images/icon6.png'
      },
      {
        text: '2-16',
        path: '../../../images/date.png'
      },
    ],
    plotData: [
      {
        text: '晴转多云',
        path: '../../../images/icon8.png'
      },
      {
        text: '晴转多云',
        path: '../../../images/icon3.png'
      },
      {
        text: '晴转多云',
        path: '../../../images/icon6.png'
      },
      {
        text: '晴转多云',
        path: '../../../images/date.png'
      },
    ],
    ec: {
      onInit: initChart
    },
    show: false,
    buttons: [{
        type: 'default',
        className: '',
        text: '地块一',
        value: 0
      },
      {
        type: 'default',
        className: '',
        text: '地块二',
        value: 1
      },
      {
        type: 'default',
        className: '',
        text: '地块三',
        value: 2
      },
      {
        type: 'default',
        className: '',
        text: '地块四',
        value: 3
      },
      {
        type: 'default',
        className: '',
        text: '地块五',
        value: 4
      }
    ],
    healthList: [
      {
        img: '../../../images/icon2.png',
        title: '武昌农业基sdfsdfsdfsdfsdfsdfsd',
        name: '玉米',
        number: '230㎡',
      },
      {
        img: '../../../images/icon2.png',
        title: '武昌农业基sdfsdfsdfsdfsdfsdfsd',
        name: '玉米',
        number: '230㎡',
      },
      {
        img: '../../../images/icon2.png',
        title: '武昌农业基sdfsdfsdfsdfsdfsdfsd',
        name: '玉米',
        number: '230㎡',
      },
      {
        img: '../../../images/icon2.png',
        title: '武昌农业基sdfsdfsdfsdfsdfsdfsd',
        name: '玉米',
        number: '230㎡',
      },
      {
        img: '../../../images/icon2.png',
        title: '武昌农业基sdfsdfsdfsdfsdfsdfsd',
        name: '玉米',
        number: '230㎡',
      },
      {
        img: '../../../images/icon2.png',
        title: '武昌农业基sdfsdfsdfsdfsdfsdfsd',
        name: '玉米',
        number: '230㎡',
      },
    ]
  },

  open: function () {
    this.setData({
      show: true
    })
  },
  close: function() {
    console.log(1111,this.data.show)
    let that = this
    setTimeout(()=> {
      that.setData({
        show: false
      })
    },130)
  },
  buttontap(e) {
    console.log(e.detail)
  },

  goPages: function (e) {
    let type = e.currentTarget.dataset['type'],
        title;
    switch (type) {
      case 'monitor':
        title = "监控详情"
        break;
      case 'news':
        title = "新闻"
        break;
      case 'newsDetails':
        title = "新闻内容"
        break;
      case 'inform':
        title = "提醒"
        break;
      case 'healthDetails':
        title = "详情"
      default:
        break;
    }
    wx.navigateTo({
      url: `/pages/home/${type}/index`,
      success: function(res) {
        wx.setNavigationBarTitle({title})
        // 通过eventChannel向被打开页面传送数据
        // res.eventChannel.emit('acceptDataFromOpenerPage', { data: 'test' })
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (typeof this.getTabBar === 'function' && this.getTabBar()) {
      console.log(12121)
      this.getTabBar().setData({
        selected: app.globalData.selected
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    var that = this;
    that.canvasRing = that.selectComponent("#canvasRing");
    that.canvasRing.showCanvasRing();
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})